<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/11/18
 * Time: 21:23
 */

use app\storage\model\SupplierModel;
use think\Db;
use think\Model;


/**
 * 获取当前时间
 *
 * @return false|string
 */
function get_current_time()
{
    return date('Y-m-d H:i:s', time());
}

/**
 * 生成唯一的单号
 * @param string $code
 * @return string
 */
function create_unique_id($code = '')
{
    return $code . date('ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
}

/**
 * 获取当前登录的管事员id
 * @return int
 */
function get_current_admin_id()
{
    return session('ADMIN_ID');
}

/**
 * 判断订单类型，是普通订单还是委外订单
 * @param $orderId
 * @return array
 */
function judge_bom_type($orderId)
{
    $flag = strpos($orderId, '+');
    if (!empty($flag)) {               //通过'+'来判定订单类型，委外订单进行字符串截取
        $order_id = strstr($orderId, '+', TRUE);
        $fentryid = substr(strstr($orderId, '+'), 1);
    } else {
        $order_id = $orderId;
        $fentryid = "";
    }
    return [$order_id, $fentryid];
}

/**
 * 生成uuid
 * @return string
 */
function create_uuid()
{
    return md5(uniqid(mt_rand(), true));
}

/**
 * ajax返回数据，用于返回网页请求
 * @param bool $success 是否成功
 * @param string $message 返回信息
 * @param null $data 返回数据
 */
function ajax_response($success = false, $message = '', $data = null)
{
    $data = [
        'success' => $success,
        'message' => $message,
        'data' => $data
    ];
    ajax_return($data);
}

/**
 * 返回ajax请求数据的json格式
 * @param $data
 */
function ajax_return($data)
{
    header('Content-Type:application/json;charset=utf-8');
    exit(json_encode($data));
}

/**
 * 生成随机字符串
 * @param int $len 长度
 * @return string
 */
function make_random_str($len = 6)
{
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!';
    $str = '';
    for ($i = 0; $i < $len; $i++) {
        $str .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $str;
}

function get_in_string($arr = [])
{
    $str = join(",", $arr);
    $inStr = "'" . str_replace(",", "','", $str) . "'";
    return $inStr;
}

function ret_template($success, $message = '', $data = '')
{
    return [
        'success' => $success,
        'message' => $message,
        'data' => $data
    ];
}


